package easy;

/**
 * @author admin
 * 1572. 矩阵对角线元素的和
 * 解题思路：逐行遍历，定义一个int型的sum来表示最终结果，将行数等于位数的数相加，假如是奇数行列，要减去一个中间数（因为被加了两次）
 * tip: &运算，两个奇数的&运算结果为1，一奇一偶的&运算结果为0
 */
public class LeeCode1572 {

    public int diagonalSum(int[][] mat) {

        int n = mat.length;
        int sum = 0;
        int mid = n/2;
        for (int i = 0; i < n; i++) {
            sum = sum + mat[i][i] + mat[i][n - 1 - i];
        }
//        System.out.println(1 & 2);
        return sum - mat[mid][mid]*(n & 1);
    }

    public static void main(String[] args) {
        int [][] mat = {{1,2,3},{4,5,6},{7,8,9}};
        System.out.println(new LeeCode1572().diagonalSum(mat));
    }

}
